package com.hopes.bizz.risk.applet.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.common.constant.RiskDicConstant;
import com.hopes.bizz.risk.hid.dto.HidChecklistTaskDTO;
import com.hopes.bizz.risk.hid.dto.RiskHidHiddenDangerDTO;
import com.hopes.bizz.risk.hid.entity.RiskHidCheckTaskEntity;
import com.hopes.bizz.risk.hid.entity.RiskHidHiddenDangerEntity;
import com.hopes.bizz.risk.hid.service.RiskHidCheckTaskRecordService;
import com.hopes.bizz.risk.hid.service.RiskHidCheckTaskService;
import com.hopes.bizz.risk.hid.service.RiskHidHiddenDangerService;
import com.hopes.bizz.risk.hid.vo.RiskHidCheckTaskVO;
import com.hopes.bizz.risk.hid.vo.RiskHidHiddenDangerVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.core.util.StringUtils;
import com.hopes.boss.common.idempotent.annotation.Idempotent;
import com.hopes.boss.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author: ljh
 * @create: 2023/7/28
 * 小程序隐患查询接口
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/appletHid")
@Tag(description = "appletHid", name = "小程序隐患查询接口")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AppletRiskHidController {

    // 隐患排查
    private final RiskHidHiddenDangerService hidHiddenDangerService;

    // 隐患检查任务
    private final RiskHidCheckTaskService riskHidCheckTaskService;

    // 隐患检查记录
    private final RiskHidCheckTaskRecordService riskHidCheckTaskRecordService;

//
//    @Operation(summary = "隐患确认列表-待确认", description = "隐患确认列表-待确认")
//    @GetMapping("/listWaitConfirm")
//    public R listWaitConfirm(RiskHidHiddenDangerFlowSearchDTO flowSearch, Page<RiskHidHiddenDangerEntity> page) {
//        return R.ok(hidHiddenDangerService.queryTodoForPage(page, HidDicConstant.HID_DANGER_STATUS_WAIT_CONFIRM.toString(), flowSearch)
//                .convert(RiskHidHiddenDangerVO::new));
//    }
//
//    @Operation(summary = "隐患确认列表-已确认", description = "隐患确认列表-已确认")
//    @GetMapping("/listConfirmed")
//    public R listConfirmed(RiskHidHiddenDangerFlowSearchDTO flowSearch, Page<RiskHidHiddenDangerEntity> page) {
//        return R.ok(hidHiddenDangerService.queryFinishForPage(page, HidDicConstant.HID_DANGER_STATUS_WAIT_CONFIRM.toString(), flowSearch)
//                .convert(RiskHidHiddenDangerVO::new));
//    }
//
//    @Operation(summary = "查询我的待整改", description = "查询我的待整改")
//    @GetMapping("/listWaitRectify")
//    public R listWaitRectify(RiskHidHiddenDangerFlowSearchDTO flowSearch, Page<RiskHidHiddenDangerEntity> page) {
//        return R.ok(hidHiddenDangerService.queryTodoForPage(page, HidDicConstant.HID_DANGER_WAIT_RECTIFICATION.toString(), flowSearch)
//                .convert(RiskHidHiddenDangerVO::new));
//    }
//
//
//    @Operation(summary = "查询我的已整改", description = "查询我的已整改")
//    @GetMapping("/listRectified")
//    public R listWaitRectified(RiskHidHiddenDangerFlowSearchDTO flowSearch, Page<RiskHidHiddenDangerEntity> page) {
//        return R.ok(hidHiddenDangerService.queryFinishForPage(page, HidDicConstant.HID_DANGER_WAIT_RECTIFICATION.toString(), flowSearch)
//                .convert(RiskHidHiddenDangerVO::new));
//    }
//
//
//    @Operation(summary = "查询我的待验收", description = "查询我的待验收")
//    @GetMapping("/listWaitCheck")
//    public R listWaitCheck(RiskHidHiddenDangerFlowSearchDTO flowSearch, Page<RiskHidHiddenDangerEntity> page) {
//        return R.ok(hidHiddenDangerService.queryTodoForPage(page, HidDicConstant.HID_DANGER_STATUS_WAIT_ACCEPTANCE.toString(), flowSearch)
//                .convert(RiskHidHiddenDangerVO::new));
//    }
//
//    @Operation(summary = "查询我的已验收", description = "查询我的已验收")
//    @GetMapping("/listChecked")
//    public R listChecked(RiskHidHiddenDangerFlowSearchDTO flowSearch, Page<RiskHidHiddenDangerEntity> page) {
//        return R.ok(hidHiddenDangerService.queryFinishForPage(page, HidDicConstant.HID_DANGER_STATUS_WAIT_ACCEPTANCE.toString(), flowSearch)
//                .convert(RiskHidHiddenDangerVO::new));
//    }


    @Operation(summary = "查询我的待检查任务", description = "查询我的待检查任务")
    @GetMapping(value = "listTodoTask")
    public R listTodoTask(Page<RiskHidCheckTaskEntity> page,RiskHidCheckTaskEntity riskHidCheckTaskEntity) {
        LambdaQueryWrapper<RiskHidCheckTaskEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(SecurityUtils.getUser()), RiskHidCheckTaskEntity::getRecorder, SecurityUtils.getUser().getId())
                .eq(RiskHidCheckTaskEntity::getTaskStatus, RiskDicConstant.RISK_HID_CHECKLIST_TASK_STATUS_TODO)
                .like(StringUtils.isNotBlank(riskHidCheckTaskEntity.getTaskName()),RiskHidCheckTaskEntity::getTaskName,riskHidCheckTaskEntity.getTaskName());
        return R.ok(riskHidCheckTaskService.page(page, wrapper).convert(RiskHidCheckTaskVO::new));
    }


    @Operation(summary = "查询我的已检查任务", description = "查询我的已检查任务")
    @GetMapping(value = "listCompletedTask")
    public R listCompletedTask(Page<RiskHidCheckTaskEntity> page,RiskHidCheckTaskEntity riskHidCheckTaskEntity) {
        LambdaQueryWrapper<RiskHidCheckTaskEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(SecurityUtils.getUser()), RiskHidCheckTaskEntity::getRecorder, SecurityUtils.getUser().getId())
                .eq(RiskHidCheckTaskEntity::getTaskStatus, RiskDicConstant.RISK_HID_CHECKLIST_TASK_STATUS_COMPLETE)
                .like(StringUtils.isNotBlank(riskHidCheckTaskEntity.getTaskName()),RiskHidCheckTaskEntity::getTaskName,riskHidCheckTaskEntity.getTaskName());
        return R.ok(riskHidCheckTaskService.page(page, wrapper).convert(RiskHidCheckTaskVO::new));
    }

    /**
     * 隐患检查任务详情
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询隐患检查任务详情", description = "通过id查询隐患检查任务详情")
    @GetMapping("/hidCheckTak/{id}")
    public R getHidCheckTakById(@PathVariable("id") Long id) {
        RiskHidCheckTaskEntity entity = riskHidCheckTaskService.getById(id);
        RiskHidCheckTaskVO vo = new RiskHidCheckTaskVO();
        BeanUtils.copyProperties(entity, vo);
        return R.ok(vo);
    }

    /**
     * 提交隐患检查任务
     *
     * @return R
     */
    @Operation(summary = "提交隐患检查任务", description = "提交隐患检查任务")
    @PostMapping("/hidCheckTak/complete")
    @Idempotent(key = "#checklistTaskDTO.id", expireTime = 5, info = "请勿重复查询")
    public R completeHidCheckTak(@RequestBody HidChecklistTaskDTO checklistTaskDTO) {
        return R.ok(riskHidCheckTaskService.completeTask(checklistTaskDTO));
    }

    /**
     * 通过taskId查询隐患检查任务的检查项
     *
     * @param taskId taskId
     * @return R
     */
    @Operation(summary = "通过taskId查询隐患检查任务的检查项", description = "通过taskId查询隐患检查任务详情")
    @GetMapping("/hidCheckTaskRecord/{taskId}")
    public R listHidCheckTakRecordByTaskId(@PathVariable("taskId") Long taskId) {
        return R.ok(riskHidCheckTaskRecordService.listByTaskId(taskId));
    }

    /**
     * 根据隐患检查项Id获取信息
     *
     * @param recordId recordId
     * @return R
     */
    @Operation(summary = "根据隐患检查项Id获取信息", description = "根据隐患检查项Id获取信息")
    @GetMapping("/hidCheckTaskRecordInfo/{recordId}")
    public R initHidInfoByRecordId(@PathVariable("recordId") Long recordId) {
        return R.ok(riskHidCheckTaskRecordService.initHidInfo(recordId));
    }

    /**
     * 提交隐患检查任务
     *
     * @return R
     */
    @Operation(summary = "提交隐患检查项", description = "提交隐患检查任务")
    @PostMapping("/hidCheckTaskRecord/saveTaskHidden")
    @Idempotent(expireTime = 5, info = "请勿重复查询")
    public R saveTaskHidden(@RequestBody RiskHidHiddenDangerDTO riskHidHiddenDangerDTO) {
        return R.ok(riskHidCheckTaskRecordService.saveTaskHidden(riskHidHiddenDangerDTO));
    }

    /**
     * 通过taskId查询隐患检查任务的异常检查项
     *
     * @param taskId taskId
     * @return
     */
    @Operation(summary = "通过taskId查询隐患检查任务的异常检查项", description = "通过taskId查询隐患检查任务的异常检查项")
    @GetMapping(value = "/hidCheckTaskExRecord/{taskId}")
    public R listHidCheckTakExRecordByTaskId(@PathVariable Long taskId) {
        return R.ok(riskHidCheckTaskRecordService.listExByTaskId(taskId));
    }


    @Operation(summary = "查询预期的隐患数据", description = "查询预期的隐患数据")
    @GetMapping(value = "/listOverdueHid")
    public R listOverdueHid(Page<RiskHidHiddenDangerEntity> page, RiskHidHiddenDangerEntity hidHiddenDanger) {
        return R.ok(hidHiddenDangerService.lambdaQuery(hidHiddenDanger)
                .le(RiskHidHiddenDangerEntity::getCorrectiveDeadline, new Date())
                .page(page).convert(RiskHidHiddenDangerVO::new));
    }
}
